package com.sgb.kjwl.viewmodel.self.publish;

import androidx.databinding.ObservableField;

import com.sgb.kjwl.AppConstant;
import com.sgb.kjwl.api.APIManager;
import com.sgb.kjwl.model.entity.supplier3.DictEntity3;
import com.sgb.kjwl.model.entity.transport.MapLongAndLatEntity;
import com.sgb.kjwl.model.entity.user.AddCarEntity3;
import com.sgb.kjwl.model.entity.user.AddressEntity;
import com.sgb.kjwl.model.entity.user.ServiceEntity;
import com.sgb.kjwl.repository.SelfRepertory;
import com.swgk.core.event.EventConstant;
import com.sgb.kjwl.view.event.LeaseCarPublishEvent;
import com.sgb.link.event.LeaseEvent;
import com.sgb.kjwl.view.event.LoginEvent;
import com.sgb.kjwl.view.ui.activity.self.publish.CarPublishActivity3;
import com.swgk.core.base.BaseViewModel;
import com.swgk.core.base.model.entity.BaseEntity;
import com.swgk.core.dialog.DialogHelper;
import com.swgk.core.util.MLog;
import com.swgk.core.util.MToast;

import org.greenrobot.eventbus.EventBus;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class CarPublishModel3 extends BaseViewModel {
    public ObservableField<AddCarEntity3> addCarEntity = new ObservableField<>(new AddCarEntity3());
    private CarPublishActivity3 activity;
    private SelfRepertory repertory;

    public CarPublishModel3(CarPublishActivity3 activity, SelfRepertory repertory) {
        this.activity = activity;
        this.repertory = repertory;
    }

//    public void getPickerCarStore(boolean init) {
//        APIManager.getInstance().getUserAPI().getDictCarStore().enqueue(new Callback<BaseEntity<List<CommonPickerEntity3>>>() {
//            @Override
//            public void onResponse(Call<BaseEntity<List<CommonPickerEntity3>>> call, Response<BaseEntity<List<CommonPickerEntity3>>> response) {
//                if (response.body() != null && response.body().getState().equals("ok")) {
//                    if (init) {
//                        activity.showChooseStorePicker(response.body().getData());
//                    } else {
//                        activity.showStorePicker(response.body().getData());
//                    }
//                }
//            }
//
//            @Override
//            public void onFailure(Call<BaseEntity<List<CommonPickerEntity3>>> call, Throwable t) {
//
//            }
//        });
//    }

    public void getCarAddition() {
        APIManager.getInstance().getUserAPI().getDictCarAddition().enqueue(new Callback<BaseEntity<List<DictEntity3>>>() {
            @Override
            public void onResponse(Call<BaseEntity<List<DictEntity3>>> call, Response<BaseEntity<List<DictEntity3>>> response) {
                if (response.body() != null && response.body().getState().equals("ok")) {
                    activity.showAdditionType(response.body().getData());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<List<DictEntity3>>> call, Throwable t) {

            }
        });
    }

    public void getAddressModel() {
        DialogHelper.showProgressDialog(activity, null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(false);
        APIManager.getInstance().getGoodsAPI().getCityList().enqueue(new Callback<BaseEntity<List<AddressEntity<List<AddressEntity<List<AddressEntity>>>>>>>() {
            @Override
            public void onResponse(Call<BaseEntity<List<AddressEntity<List<AddressEntity<List<AddressEntity>>>>>>> call, Response<BaseEntity<List<AddressEntity<List<AddressEntity<List<AddressEntity>>>>>>> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body().getState().equals(AppConstant.REQEUST_SECCUESS)) {
                    activity.showPickupAreraPop(response.body().getData());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<List<AddressEntity<List<AddressEntity<List<AddressEntity>>>>>>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
                MLog.d(AppConstant.API_LOG, "error " + t.getMessage());
            }
        });
    }

    public void getBackAreaModel() {
        DialogHelper.showProgressDialog(activity, null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(false);
        APIManager.getInstance().getGoodsAPI().getCityList().enqueue(new Callback<BaseEntity<List<AddressEntity<List<AddressEntity<List<AddressEntity>>>>>>>() {
            @Override
            public void onResponse(Call<BaseEntity<List<AddressEntity<List<AddressEntity<List<AddressEntity>>>>>>> call, Response<BaseEntity<List<AddressEntity<List<AddressEntity<List<AddressEntity>>>>>>> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body().getState().equals(AppConstant.REQEUST_SECCUESS)) {
                    activity.showBackAreraPop(response.body().getData());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<List<AddressEntity<List<AddressEntity<List<AddressEntity>>>>>>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
                MLog.d(AppConstant.API_LOG, "error " + t.getMessage());
            }
        });
    }

    public void getPickupLocationModel(String provinceNo, String cityNo, String districtNo) {
        APIManager.getInstance().getDriverAPI().getLocationLL(repertory.getAreaCode(), provinceNo, cityNo, districtNo).enqueue(new Callback<BaseEntity<MapLongAndLatEntity>>() {
            @Override
            public void onResponse(Call<BaseEntity<MapLongAndLatEntity>> call, Response<BaseEntity<MapLongAndLatEntity>> response) {
                if (response.body() == null) return;
                if (response.body().getState().equals(AppConstant.REQEUST_SECCUESS)) {
                    activity.showPickupLocation(response.body().getData());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<MapLongAndLatEntity>> call, Throwable t) {
            }
        });
    }

    public void getReturLnocationModel(String provinceNo, String cityNo, String districtNo) {
        APIManager.getInstance().getDriverAPI().getLocationLL(repertory.getAreaCode(), provinceNo, cityNo, districtNo).enqueue(new Callback<BaseEntity<MapLongAndLatEntity>>() {
            @Override
            public void onResponse(Call<BaseEntity<MapLongAndLatEntity>> call, Response<BaseEntity<MapLongAndLatEntity>> response) {
                if (response.body() == null) return;
                if (response.body().getState().equals(AppConstant.REQEUST_SECCUESS)) {
                    activity.showReturnLocation(response.body().getData());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<MapLongAndLatEntity>> call, Throwable t) {
            }
        });
    }

    public void publishModel() {
        DialogHelper.showProgressDialog(activity, null, "发布中...", 0, false, null).setCanceledOnTouchOutside(false);
        APIManager.getInstance().getUserAPI().pubulishCarInfo(addCarEntity.get())
                .enqueue(new Callback<BaseEntity>() {
                    @Override
                    public void onResponse(Call<BaseEntity> call, Response<BaseEntity> response) {
                        DialogHelper.dismissProgressDialog();
                        if (response.body() != null && response.body().getState().equals(AppConstant.REQEUST_SECCUESS)) {
                            if (addCarEntity.get().getId() != null) {
//                                MToast.showToast(activity, "编辑成功");
                                EventBus.getDefault().post(LoginEvent.make().setMessageTag(EventConstant.EVENT_FINISH_H5));//如果是从h5编辑跳转到app的,在编辑完成后关闭h5
                            } else {
//                                MToast.showToast(activity, "发布成功");
                            }
                            EventBus.getDefault().post(LeaseCarPublishEvent.make().setMessageTag(EventConstant.EVENT_CAR_PUBLISH_FINISH));
                            EventBus.getDefault().post(LeaseEvent.make().setMessageTag(EventConstant.EVENT_CAR_PUBLISH_FINISH));
                            activity.showSuccess();
                            activity.finish();
                        } else {
                            MToast.showToast(activity, "发布失败");
                            activity.showReNext();
                        }
                    }

                    @Override
                    public void onFailure(Call<BaseEntity> call, Throwable t) {
                        MToast.showToast(activity, "发送服务器失败");
                        DialogHelper.dismissProgressDialog();
                        activity.showReNext();
                    }
                });
    }

    /**
     * 获取客服列表
     */
    public void getCustomerOrgDatas() {
        DialogHelper.showProgressDialog(activity, null, "加载中...", 0, false, null).setCanceledOnTouchOutside(false);
        APIManager.getInstance().getMachineAPI().getCustomerOrg().enqueue(
                new Callback<BaseEntity<List<ServiceEntity>>>() {
                    @Override
                    public void onResponse(Call<BaseEntity<List<ServiceEntity>>> call, Response<BaseEntity<List<ServiceEntity>>> response) {
                        DialogHelper.dismissProgressDialog();
                        if (response.body() == null) return;
                        activity.getCustomerOrgDataS(response.body().getData());
                    }

                    @Override
                    public void onFailure(Call<BaseEntity<List<ServiceEntity>>> call, Throwable t) {
                        DialogHelper.dismissProgressDialog();
                    }
                }
        );
    }

    //判断公司是否有审批
    public void haveApprovalOrNot(String companyNo,String type) {
        DialogHelper.showProgressDialog(activity, "加载中...").setCanceledOnTouchOutside(false);
        APIManager.getInstance().getLeaseAPI().haveApprovalOrNot(companyNo,type).enqueue(new Callback<BaseEntity>() {
            @Override
            public void onResponse(Call<BaseEntity> call, Response<BaseEntity> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null) return;
                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    try {//接口返回数据不标准,只能自行转换
                        JSONObject jsonObject = new JSONObject(response.body().getData().toString());
                        String str = jsonObject.getString("data");
                        //0无审批流 1 有审批流
                        activity.showPublishTip("1".equals(str));
                    } catch (JSONException e) {
                        e.printStackTrace();
                        MToast.showToast(activity, "服务异常");
                    }

                }
            }

            @Override
            public void onFailure(Call<BaseEntity> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
            }
        });
    }
}
